package com.web.config;
import com.web.security.filter.CustomizeAuthenticationFilter;
import com.web.security.handler.CustomizeAccessDeniedHandler;
import com.web.security.handler.CustomizeAuthenticationEntryPoint;
import com.web.security.handler.CustomizeAuthenticationFailureHandler;
import com.web.security.handler.CustomizeAuthenticationSuccessHandler;
import com.web.security.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author: wws
 * @Date: 2021/2/2 23:29
 * describe:
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //用户未登录
    @Autowired
    CustomizeAuthenticationEntryPoint authenticationEntryPoint;

    //自定义登录
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    //用户登录失败
    @Autowired
    CustomizeAuthenticationFailureHandler authenticationFailureHandler;
    //用户登录成功
    @Autowired
    CustomizeAuthenticationSuccessHandler authenticationSuccessHandler;
    //用户无权限
    @Autowired
    CustomizeAccessDeniedHandler accessDeniedHandler;

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.cors().and().csrf().disable();
//        http.authorizeRequests(authorizeRequests ->
//                // 所有请求均放过, spring security 就没有什么用了
//                // anyRequest() 限定任意的请求
//                // permitAll() 无条件允许访问
//                authorizeRequests.anyRequest().permitAll()
//        );
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler)
                .and().
                authorizeRequests()
                .antMatchers("/login.html").anonymous()
                .anyRequest().authenticated()
                .and().formLogin()
                .and().sessionManagement().maximumSessions(1);
        http.addFilterAt(customizedAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setPasswordEncoder(bCryptPasswordEncoder());
        provider.setUserDetailsService(userDetailsService);
        provider.setHideUserNotFoundExceptions(false);
        return provider;
    }

    @Bean
    CustomizeAuthenticationFilter customizedAuthenticationFilter() throws Exception {
        CustomizeAuthenticationFilter filter = new CustomizeAuthenticationFilter();
        filter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        filter.setAuthenticationFailureHandler(authenticationFailureHandler);
        filter.setAuthenticationManager(authenticationManagerBean());
        return filter;
    }

    /**
     * 强散列哈希加密实现
     */
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 身份认证接口
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
        auth.authenticationProvider(authenticationProvider());
    }


}
